$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var protype;
					var totalCount = $("#totalCount").val();

					var pSize = $("#pageSize").val();
					;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();

					if (pageCount == null || pageCount == ""
							|| pageCount == undefined) {
						pageCount = 1;

					}

					$("#protypeDetail").on("bs.shown.modal", function() {
						protype.unignore();
					})
					vaildation();

					$("#optBtn").delegate('#saveBtn', 'click', function() {

						$("#protypeDetail").attr("method", "post");

						var typeId = $("#typeId").val();

						if (typeId == "") {

							$("#protypeDetail").attr("action", "save.do");
						} else {

							$("#protypeDetail").attr("action", "update.do");
						}
						$("#protypeDetail").submit();

					});

					$('#pagination2')
							.jqPaginator(
									{
										totalPages : pageCount,
										visiblePages : 10,
										currentPage : 1,

										/*
										 * first: '<li class="first"><a
										 * href="javascript:void(0);">第一页</a></li>',
										 */
										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										/*
										 * last: '<li class="last"><a
										 * href="javascript:void(0);">Last</a></li>',
										 */
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											// alert("pageIndex="+num);
											// alert("type="+type);
											$("#pageIndex").val(num);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize
														},
														cache : false,
														success : function(data) {

															$(
																	"#protypes tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;
															for (i = 0; i < results.length; i++) {
																var protype = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='typeId' class='checkbox' type='checkbox' value='"
																		+ protype.id
																		+ "'/></td>"
																		+ "<td>"
																		+ protype.typeCode
																		+ "</td>"
																		+ "<td>"
																		+ protype.typeName
																		+ "</td>"
																		+ "<td>"
																		+ protype.note
																		+ "</td>"
																rowContent = rowContent
																		+ "<td><button   name='showList' objId='"
																		+ protype.id
																		+ "'class='btn btn-sm-block'>查看产品列表</button></td>"
																		+ "<td><button name='editBtn' objId='"
																		+ protype.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "</tr>";
																$("#protypes")
																		.append(
																				rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#protypes").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='typeId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#protypes").delegate('[name="editBtn"]', 'click',
							function() {
								var typeId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : typeId
									},
									cache : false,

									success : function(data) {

										loadProType(data);
										$("#protypeDialog").modal('show');
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#protypes").delegate(
							'[name="showList"]',
							'click',
							function() {
								var typeId = $(this).attr("objId");

								var editUrl = basePath
										+ "/manage/proInfor/queryByProType.do";
								document.location.href = editUrl + "?typeId="
										+ typeId;

							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="typeId"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								var ids = "";
								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#tools").delegate('#addUser', 'click', function() {

						$("#protypeDialog input").each(function() {
							$(this).prop("value", null);
							$(this).removeAttr("readonly");

						});
						$("#typeCode").attr("readonly", true);
						$("#protypeDialog textarea").each(function() {
							$(this).prop("value", null);

						});
						$("#protypeDialog").modal('show');

					});

					$("#typeName")
							.delegate(
									$(this),
									'change',
									function() {
										var proName = $(this).val();
										var typeId = $("#typeId").val();
										
										if(typeId==null||typeId==""||typeId==undefined){
										
										var queryUrl = encodeURI("queryProCodeByName.do?proName='"
												+ proName + "'");
										
										$.ajax({
											url : queryUrl,
											dataType : 'text',

											cache : false,
											success : function(data) {

												$("#typeCode").val(data);

											},
											error : function(html) {
												alert("获取类型编号失败");

											}
										});
										}
									});

					function loadProType(proType) {

						$("#typeId").val(proType.id);
						$("#typeCode").val(proType.typeCode);
						$("#typeName").val(proType.typeName);
						$("#note").val(proType.note);
						protype.ignore();

					}

					function vaildation() {
						protype = $(".jqtransform")
								.Validform(
										{
											tiptype : function(msg, o, cssctl) {

												if (!o.obj.is("form")) {
													var tds = o.obj.text();

													var objtip = o.obj
															.siblings(".Validform_checktip");
													cssctl(objtip, o.type);
													objtip.text(msg);

												} else {
													if (infoObj.is(":visible")) {
														return;
													}
													var left = o.obj.offset().left, top = o.obj
															.offset().top;

													infoObj.css({
														left : left + 170,
														top : top - 45
													}).show().animate({
														top : top - 35
													}, 200);
												}
											}
										});
					}

					function refresh() {
						window.location.reload();
					}
				});
